//
//  BuyPreViewController.swift
//  SCA
//
//  Created by tjun on 2025/9/19.
//

import UIKit

class BuyPreViewController: UIViewController {

    private var maskView: UIButton!
    private var contentView: UIView!
    let model: PreviewBuyComputingPowerModel
    var onCommit: ((String) -> Void)?
    
    let passwordTextField = LabeledTextField()
    let confirmButton = MainButton(type: .system, title: "熔铸".localizedString, font: UIFontMake(16), textColor: .white)

    init(model: PreviewBuyComputingPowerModel) {
        self.model = model
        super.init(nibName: nil, bundle: nil)
        modalPresentationStyle = .custom
    }
    
    deinit {
        printf(#function)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        view.backgroundColor = .clear
        
        maskView = UIButton(frame: view.bounds).addTo(view)
        maskView.backgroundColor = .black.withAlphaComponent(0)
        
        let contentW = view.width - 80
        let contentH: CGFloat = 350
        
        contentView = UIView().addTo(view)
        contentView.backgroundColor = UIColorMake(0x212529)
        contentView.frame = CGRectMake((view.width - contentW) * 0.5, -contentH , contentW, contentH)
        contentView.addCorner(radius: 15)
        
        let titleLabel = UILabel(text: "购买摘要预览".localizedString, font: UIFontSemiboldMake(16), textColor: .white).addTo(contentView)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalToSuperview().offset(25)
        }
        
        let closeButton = UIButton(type: .system)
        closeButton.setImage(UIImage(systemName: "xmark"), for: .normal)
        closeButton.tintColor = .white
        closeButton.addTarget(self, action: #selector(closeButtonTapped), for: .touchUpInside)
        contentView.addSubview(closeButton)
        closeButton.snp.makeConstraints { make in
            make.right.equalTo(-20)
            make.centerY.equalTo(titleLabel)
            make.width.height.equalTo(30)
        }
        
        let lineView = UIView().addTo(contentView)
        lineView.backgroundColor = UIColor.white.withAlphaComponent(0.3)
        lineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(20)
            make.height.equalTo(1)
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
        }
        
        let margin = 14

        let wayLabel = UILabel(font: UIFontMake(16), textColor: UIColorMake(0xC2C2C2)).addTo(contentView)
        wayLabel.snp.makeConstraints { make in
            make.left.equalTo(titleLabel)
            make.top.equalTo(lineView.snp.bottom).offset(25)
        }
        
        let wayCoinImageView = UIImageView().addTo(contentView)
        wayCoinImageView.snp.makeConstraints { make in
            make.left.equalTo(wayLabel.snp.right).offset(12)
            make.width.height.equalTo(36)
            make.centerY.equalTo(wayLabel)
        }
        
        let wayCoinNameLabel = UILabel(font: UIFontBoldMake(16), textColor: UIColor.white).addTo(contentView)
        wayCoinNameLabel.snp.makeConstraints { make in
            make.left.equalTo(wayCoinImageView.snp.right).offset(12)
            make.top.equalTo(lineView.snp.bottom).offset(25)
        }
        
        let aprLabel = UILabel().addTo(contentView)
        aprLabel.snp.makeConstraints { make in
            make.left.equalTo(wayLabel)
            make.top.equalTo(wayLabel.snp.bottom).offset(margin)
        }
        
        let powerLabel = UILabel().addTo(contentView)
        powerLabel.snp.makeConstraints { make in
            make.left.equalTo(wayLabel)
            make.top.equalTo(aprLabel.snp.bottom).offset(margin)
        }
        
        let evLabel = UILabel().addTo(contentView)
        evLabel.snp.makeConstraints { make in
            make.left.equalTo(wayLabel)
            make.top.equalTo(powerLabel.snp.bottom).offset(margin)
        }
        
        let paymentLabel = UILabel().addTo(contentView)
        paymentLabel.snp.makeConstraints { make in
            make.left.equalTo(wayLabel)
            make.top.equalTo(evLabel.snp.bottom).offset(margin)
        }
        
        let investmentLabel = UILabel().addTo(contentView)
        investmentLabel.snp.makeConstraints { make in
            make.left.equalTo(wayLabel)
            make.top.equalTo(paymentLabel.snp.bottom).offset(margin)
        }
        
        passwordTextField.titleLabel.textColor = UIColorMake(0xC2C2C2)
        passwordTextField.contentView.snp.remakeConstraints { make in
            make.top.equalTo(passwordTextField.titleLabel.snp.bottom).offset(8)
            make.height.equalTo(AppStyle.size.smallButtonHeight.scaled)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        passwordTextField.titleAttributedText = "二级安全密码*".localizedString.attributedString(rangeString: "*", rangeColor: UIColorMake(0xFF0000))
        passwordTextField.textField.attributedPlaceholder = NSAttributedString(string: "代码".localizedString, attributes: [NSAttributedString.Key.foregroundColor: UIColorMake(0x7F8292)])
        passwordTextField.textField.keyboardType = .asciiCapable
        passwordTextField.textFieldCornerRadius = 10
        contentView.addSubview(passwordTextField)
        passwordTextField.textField.addTarget(self, action: #selector(textFieldDidChange(_ :)), for: .editingChanged)
        passwordTextField.snp.makeConstraints { make in
            make.top.equalTo(investmentLabel.snp.bottom).offset(margin)
            make.left.right.equalToSuperview().inset(20)
        }

        confirmButton.addTo(contentView)
        confirmButton.isEnabled = false
        confirmButton.addCorner(radius: 10)
        confirmButton.addTarget(self, action: #selector(confirmButtonTapped), for: .touchUpInside)
        confirmButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(passwordTextField)
            make.height.equalTo(AppStyle.size.smallButtonHeight.scaled)
            make.top.equalTo(passwordTextField.snp.bottom).offset(margin)
        }

        wayLabel.text = model.title
        wayCoinImageView.setImage(url: model.playCoinUrl)
        wayCoinNameLabel.text = model.playCoinName
        
        aprLabel.attributedText = "\(model.apr.title): \(model.apr.display)".attributedString(rangeStrings: "\(model.apr.title):", rangeColor: UIColorMake(0xC2C2C2), rangeFont: UIFontMediumMake(16), color: UIColor.white, font: UIFontSemiboldMake(16))
        
        powerLabel.attributedText = "\(model.power.title): \(model.power.display)".attributedString(rangeStrings: "\(model.power.title):", rangeColor: UIColorMake(0xC2C2C2), rangeFont: UIFontMediumMake(16), color: UIColor.white, font: UIFontSemiboldMake(16))

        evLabel.attributedText = "\(model.apr.title): \(model.ev.display)".attributedString(rangeStrings: "\(model.ev.title):", rangeColor: UIColorMake(0xC2C2C2), rangeFont: UIFontMediumMake(16), color: UIColor.white, font: UIFontSemiboldMake(16))

        
        paymentLabel.attributedText = "\(model.apr.title): \(model.payment.display)".attributedString(rangeStrings: "\(model.payment.title):", rangeColor: UIColorMake(0xC2C2C2), rangeFont: UIFontMediumMake(16), color: UIColor.white, font: UIFontSemiboldMake(16))

        investmentLabel.attributedText = "\(model.investment.title): \(model.investment.display)".attributedString(rangeStrings: "\(model.investment.title):", rangeColor: UIColorMake(0xC2C2C2), rangeFont: UIFontMediumMake(16), color: UIColor.white, font: UIFontSemiboldMake(16))
       
        view.layoutIfNeeded()
        
        contentView.height = confirmButton.maxY + 20
        
    }
    
    @objc func textFieldDidChange(_ sender: UITextField) {
        if let password = sender.text, password.count >= 6 {
            confirmButton.isEnabled = true
        } else {
            confirmButton.isEnabled = false
        }
    }
    
    @objc func confirmButtonTapped() {
        guard let password = passwordTextField.textField.text, password.count >= 6 else {
            return
        }
        dismiss {[weak self] in
            self?.onCommit?(password)
        }
    }

    @objc func closeButtonTapped() {
        dismiss()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func showIn(_ controller: UIViewController) {
        controller.present(self, animated: false, completion: nil)
    }
 
    private func dismiss(completion: (() -> Void)? = nil) {
        UIView.animate(withDuration: 0.25) {
            self.contentView.alpha = 0
            self.maskView.backgroundColor = .black.withAlphaComponent(0)
            self.contentView.y = self.contentView.y + 100
        } completion: { finishi in
            self.dismiss(animated: false, completion: completion)
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.8, options: .curveEaseIn) {
            self.maskView.backgroundColor = .black.withAlphaComponent(0.5)
            self.contentView.y = (self.view.height - self.contentView.height) * 0.5 - 35
        }
    }
    

}
